/**
 * CoffeeMilk (C) 2010 Amen Hamdan
 * 
 * See the NOTICE file distributed with this work for additional 
 * information regarding copyright ownership.  This file is licenses
 * to you under the Apache License, Version 2.0 (the "License"); you 
 * may not use this file except in compliance with the License.  You 
 * may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *     
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 
 * implied. See the License for the specific language governing 
 * permissions and limitations under the License.    
 * 
 */
package ameen.rtm.coffeemilk;

import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.UIManager;

import ameen.rtm.coffeemilk.persistency.PersistencyManager;
import ameen.rtm.coffeemilk.persistency.SettingsStore;
import ameen.rtm.coffeemilk.setup.SetupWizard;
import ameen.rtm.coffeemilk.ui.MainUiFrame;
import ameen.rtm.lib.RtmService;

public class CoffeMilkApp {
	public static final String APP_VERSION = "0.01";
	
	private RtmService fRtmService;
	private SettingsStore fSettings;
	
	public void initApp() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		fSettings = PersistencyManager.readSettings();
		fRtmService = new RtmService(fSettings);
		
		if(fRtmService.checkSettings()) {
			loadMainWindow();
		} else {
			showSettingsWizard();
		}
	}

	public void showSettingsWizard() {
		SetupWizard setupWizard = new SetupWizard();
		setupWizard.startSetupWizard(fRtmService, fSettings);

		setupWizard.getSetupWizardFrame().addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				loadMainWindow();
			}
		});
	}
	
	public final void loadMainWindow() {
		MainUiFrame mainUiFrame = new MainUiFrame();
		mainUiFrame.setVisible(true);
		mainUiFrame.setRtmService(fRtmService);
		mainUiFrame.setSettings(fSettings);
		mainUiFrame.restoreSize();
		mainUiFrame.showLoadingDialog();
		mainUiFrame.loadMainWindow();
		
	}
	
	public static void main(String[] args) {
		CoffeMilkApp coffeMilkApp = new CoffeMilkApp();
		coffeMilkApp.initApp();
	}
}
